package com.sc.nft.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.sc.nft.dao.DeviceNoBackListDao;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.service.DeviceNoBackListService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

import static net.sf.jsqlparser.parser.feature.Feature.values;


@Slf4j
@RequiredArgsConstructor
@Service
public class DeviceNoBackListServiceImpl implements DeviceNoBackListService {

    private final DeviceNoBackListDao deviceNoBackListDao;
    private final RedisTemplate redisTemplate;

    @Override
    public boolean checkByDeviceNo(String deviceNo) {
        //返回true 则是封禁
        //先查redis redis没有值 则在数据获取数据插入到redis
        if(redisTemplate.hasKey(RedisKeyEnum.DEVICE_BACK_LIST.getKey())){
            return redisTemplate.opsForSet().isMember(RedisKeyEnum.DEVICE_BACK_LIST.getKey(), deviceNo);
        }else {
            List<String> deviceNoList = deviceNoBackListDao.getDeviceNoList();

            if(CollUtil.isEmpty(deviceNoList)){
                return Boolean.FALSE;
            }

            if(deviceNoList.contains(deviceNo)){
                redisTemplate.delete(RedisKeyEnum.DEVICE_BACK_LIST.getKey());
                for (String newDeviceNo : deviceNoList) {
                    redisTemplate.opsForSet().add(RedisKeyEnum.DEVICE_BACK_LIST.getKey(),newDeviceNo);
                }

                return Boolean.TRUE;
            }

        }


        return Boolean.FALSE;
    }
}
